import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
  getDepartMent
} from '../../../service/mian/system/system'
const systemModule = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList: []
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, list: any[]) {
      state.roleList = list
    },
    changeRoleCount(state, count: number) {
      state.roleCount = count
    },
    changeGoodsList(state, list: any[]) {
      state.goodsList = list
    },
    changeGoodsCount(state, count: number) {
      state.goodsCount = count
    },
    changeMenuList(state, list: any[]) {
      state.menuList = list
    },
    changeMenuCount(state, count: number) {
      state.menuCount = count
    },
    changeDepartmentList(state, list: any[]) {
      state.departmentList = list
    },
    changeDepartmentData(state, list: any[]) {
      state.departmentList = list
    }
  },
  getters: {
    // 列表数据
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    // 列表数量
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`

      // 2.对页面发送请求
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)

      // 3.将数据存储到state中
      const { list, totalCount } = pageResult.data

      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}List`, list) //数据
      commit(`change${changePageName}Count`, totalCount) //数量
    },
    async deletePageDataAction({ dispatch }, payload: any) {
      // pageName
      // id
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      // 调用删除网络请求
      await deletePageData(pageUrl)
      // 重新渲染页面
      dispatch('getPageListAction', {
        pageName: pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    // 编辑数据的请求
    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `${pageName}`
      await createPageData(pageUrl, newData)
      // 重新渲染页面
      dispatch('getPageListAction', {
        pageName: pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    // 新建数据的请求
    async editPageDataAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      console.log(editData)
      console.log(id)
      const pageUrl = `${pageName}/${id}`
      await editPageData(pageUrl, editData)
      // 重新渲染页面
      dispatch('getPageListAction', {
        pageName: pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async departmentPageData({ commit }, payload: any) {
      const pageData = await getDepartMent(payload)
      commit('changeDepartmentData', pageData.data.list)
    }
  }
}
export default systemModule
